<?php
require_once dirname(__FILE__).'/global.php';
try {
    /*
       path/parameter/query parsing
       It's socalled rewrite in Web environment
    */
    if (!isset($_SERVER['REQUEST_METHOD'])) {//cli
        if ($_SERVER['argc']>=2) {
            $action=$_SERVER['argv'][1];
        } else {
            $action='usage';
        }
    } else {//http

        if (preg_match('#^/test.*#',$sn,$m)) {
            new C_Test;
            return;
        }


        url_parse();
        $action='404';
        if (!empty($_REQUEST['act'])) {
            $action=$_REQUEST['act'];
        } else if (cg('www_sf_uri')=='/'
        || cg('www_sf_uri')=='/index.php'
        ) {
            $action='index';
        } else {
        }
    }

    /*servlet*/
    if (!isset($_SERVER['REQUEST_METHOD'])) {//cli
        if ($action=='fetchpep') {
        } elseif ($action=='') {
        } else {
        }
    } else if ($_SERVER['REQUEST_METHOD']=='POST') {//http post
        if ($action=='') {
        } elseif ($action=='') {
        } else {
        }
        redirect(cg('www_root_no_host'));
    } else if (
    $_SERVER['REQUEST_METHOD']=='GET'
    || $_SERVER['REQUEST_METHOD']=='HEAD'
    ) {//http get
        $host=parse_url(cg('www_root'),PHP_URL_HOST);
        if ($_SERVER['HTTP_HOST']!==$host) {
            redirect('http://'.$host.$_SERVER['SCRIPT_NAME'],301);
        }
        MVC_Clear_M();
        if ($action=='404') {
            sf_404();
        } else {
            $bbste->render($action);
        }
    } else {
        die('PUT/DELETE ...');
    }
} catch (Exception $e) {
    header("HTTP/1.1 503 Service Unavailable");
    if (DEBUG_MODE) {//output the technical detail
        echo $e->getMessage();
    } else {//output user-friendly message
    }
}
function sf_404() {
    MVC_Clear_M();
    global $bbste;
    header("HTTP/1.1 404 Not Found");
    $bbste->render('404');
    die;
}

function MVC_Clear_M() {
    $obj_keys=array('bbsjk');
    foreach ($obj_keys as $k) {
        if (isset($GLOBALS[$k])) {
            unset($GLOBALS[$k]);
        }
    }
}
